package com.nuo.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;


public class ConnectionManager {
	private static Logger log = Logger.getLogger(ConnectionManager.class);
	
	private static String driver;
	private static String url;
	private static String username;
	private static String password;
	
	private static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
	
	private static boolean hasInit = false;
	
	public static void init(String dbtype, String url, String username, String password){
		if(!hasInit){
			if("oracle".equals(dbtype)){
				try {
					Class.forName(ORACLE_DRIVER);
					ConnectionManager.url = url;
					ConnectionManager.username = username;
					ConnectionManager.password = password;
				} catch (ClassNotFoundException e) {
					log.error("找到不到oracle的jdbc的jar", e);
				}
				
			}else if("mysql".equals(dbtype)){
				try {
					Class.forName(MYSQL_DRIVER);
					ConnectionManager.url = url;
					ConnectionManager.username = username;
					ConnectionManager.password = password;
				} catch (ClassNotFoundException e) {
					log.error("找到不到mysql的jdbc的jar", e);
				}
			}
		}
	}
	
	public static Connection getConnectionFromJdbc(){
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			log.error("获得数据库连接错误，参数为：" + url +","+ username +","+ password, e);
			throw new RuntimeException("获得连接失败");
		}
		return con;
	}

	
}
